import { defineStore } from 'pinia';
import { Dictionary } from '../models';

const filterDictList = (data: Dictionary[], type: string) => {
  return data.filter((item: Dictionary) => item.type === type);
};

const dictListToMap = (list: Dictionary[]) => {
  const ob: { [key: string]: any } = {};
  list.forEach((item: any) => {
    ob[item.value] = item.text;
  });
  return ob;
};

/**
 * 字典
 * @methods fetchDicts 获取所有字典值
 */
export const useDictsStore = defineStore('dicts', {
  state: () => ({
    data: [],
  }),
  getters: {
    contractTypes(state) {
      return filterDictList(state.data, 'ContractType');
    },
    contractTypeMap() {
      return dictListToMap(this.contractTypes as any);
    },
    projectTypes(state) {
      return filterDictList(state.data, 'ProjectType');
    },
    projectTypeMap() {
      return dictListToMap(this.projectTypes as any);
    },
    supplierTypes(state) {
      return filterDictList(state.data, 'SupplierType');
    },
    supplierTypeMap() {
      return dictListToMap(this.supplierTypes as any);
    },
  },
  actions: {

    async refresh() {
    },
  },
});
